#!/usr/bin/env python
# -*- coding:utf-8 -*-
# projectname: pythoncode
# author: lzf time:2022/1/19
# name : 010三创购物系统
# 定义商品列表信息
good_list = [{'good': '鸭头', 'pirce': '6'}, {'good': '鸭腿', 'pirce': '8'},
             {'good': '鸭脖', 'pirce': '6'}, {'good': '鸭爪', 'pirce': '3'},
             {'good': '鸭翅', 'pirce': '7'}, {'good': '猪蹄', 'pirce': '15'}]
# 初始化购物车
shopping_carlist = []
money = 30
flag = 0
#注册账户
def register():
    print('注册账户'.center(60, '*'))
    print('您可以输入exit直接退出！'.ljust(60, '*'))
    global account
    global password
    account = input("请设定您的账号：")
    while account.lower() != 'exit':
        password = input("请设置您的密码：")
        password_inp = input("请重新确认您的密码：")
        if password != 'exit' and password_inp != 'exit':
            if password == password_inp:
                print('恭喜你注册成功！！'.center(30, '*'))
                login()
                break
            else:
                password = input("请输入您的账号：")
                password_inp = input("输入密码与原密码有误，请重新输入：")
        else:
            break
#账户登录
def login():
    print('登录账号'.center(60, '~'))
    print('您可以输入exit直接退出！'.ljust(60, '~'))
    while True:
        user_account = input("请输入您的账号：")
        if user_account.lower() != 'exit':
            user_pwd = input("请输入您的密码：")
            if user_pwd.lower() != 'exit':
                if (user_account, user_pwd)==(account, password):
                    print('登录成功！！'.center(60, '^'))
                    showGoods()
                    break
                else:
                    print('账号或密码有误，请重新输入'.center(60, '^'))
            else:
                break
        else:
            break
#展示商品
def showGoods():
    print('欢迎来到三创卤味购物系统'.center(40, '❤'))
    print(f"您的余额为：{money}元！！！！！")
    print("{:<8}{:<8}".format("商品名单", "商品价格"))
    for i in range(0, len(good_list)):
        for value in good_list[i].values():
            print(f"{value.ljust(10,' ')}",end='')
        print()
    print('❤'*30)
    temp = input("请按任意键继续...")
    choice_act()
#操作选择
def choice_act():
    while True:
        print('*'*60)
        print("请选择接下来您的操作：")
        print(f"1------->查看购物车")
        print(f"2------->购买商品")
        print(f"3------->结算订单")
        choice = input("请输入你的选择：")
        if choice == '1' and choice != 'exit':
            order_list()
            temp = input("请输入任意键继续...")
        elif choice == '2' and choice != 'exit':
            buy_goods()
            break
        elif choice == '3' and choice != 'exit':
            pay_order()
            break
        elif choice == 'exit':
            break
        else:
            print("输入不合法，请重新输入！")

#购买商品
def buy_goods():
    print("{:<8}{:<8}".format("商品名单", "商品价格"))
    for i in range(0, len(good_list)):
        for value in good_list[i].values():
            print(f"{value.ljust(10, ' ')}", end='')
        print()
    temp = []
    while True:
        good = input("请输入你要购买的商品：")
        num = input("请输入购买的数量：")
        if good.lower() == 'exit' or num.lower() == 'exit':
            break
        else:
            for i in good_list:       #将good_list中的的key，value放入到新的购物车列表中
                for j in i.values():
                    temp.append(j)
            if good in temp and num.isdigit():
                shopping_carlist.append([good, num])
            else:
                print("输入不合法，请重新输入！")
        print("继续购物-->1;进行其他操作-->2")
        option = input("请输入你的选择：")
        if option == '1' and option != 'exit':
            continue
        elif option == '2' and option != 'exit':
            choice_act()
            break
        elif option == 'exit':
            break
        else:
            print("输入不合法")
#查看购物车
def order_list():
    temp = {}
    lis = []
    total = 0
    print("{:<8}{:<11}{:<11}".format( "商品名称", "商品价格(元/个)", "购买数量(个)"))
    for i in range(len(good_list)):
        for j in good_list[i].values():
            lis.append(j)
        temp[lis[0]] = lis[1] #获取列表中每个字典中的value值，再把它组成新的字典
        lis.clear()           #清空列表
    for i in shopping_carlist:
        good = i[0]
        num = i[1]
        price = temp[good]
        total += int(price)*int(num)
        print("{:<12}{:<12}{:<21}".format(good, price, num))
    print("{:<12}{:<12}".format('总计(元):', total))
    print("{:<12}{:<12}".format('余额(元):', money-total))
#结算订单
def pay_order():
    global flag
    flag = 0
    print("正在为您结算订单！")
    print(f"您的余额为{money}元，请慎重剁手！！")
    total = 0
    temp ={}
    lis = []
    needMoney = 0
    for i in range(len(good_list)):
        for j in good_list[i].values():
            lis.append(j)
        temp[lis[0]] = lis[1]
        lis.clear()
    for i in shopping_carlist:
        good = i[0]
        num = i[1]
        price = temp[good]
    for i in shopping_carlist:
        good = i[0]
        needMoney = int(i[1]) * int(temp[good])
        total += needMoney
    while True:
        budget = int(input("请输入您的预算："))
        if budget <= money:
            if total < budget < money:
                print(f"您这次消费在预算之内！本次消费为{total}元，您的余额为{money-total}元")
                print('{:*^140}'.format("这是你的订单，请注意查收！欢迎下次光临三创卤味店！"))
                order_list()
                flag +=1
                break
            elif budget < total < money:
                print(f"您这次购物在预算之外，本次消费为{total}元，您的余额为{money-total}元")
                print('{:*^140}'.format("这是您的订单，请注意查收！欢迎下次光临三创卤味店！"))
                order_list()
                flag += 1
                break
            else:
                print("您请余额不足，请及时充值")
                break
        else:
            print("您的预算大于您的余额！请重新输入您的预算！")

register()
if flag == 0:
    print("欢迎下次光临三创卤味店！！")
    order_list()


